package org.sja.log2traffic;

import org.sja.log2traffic.DataCalculator;
import org.sja.log2traffic.dataStructures.DateAndResponseTimeEntity;
import org.sja.log2traffic.dataStructures.ILogData;
import org.sja.log2traffic.dataStructures.INewLogInfo;
import org.sja.log2traffic.logLineParsers.SimpleLogLineParser;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class RespondentSaveResultTest {
	
	private DataCalculator dataCalculator = new DataCalculator(); 
	private NewLogInfoMock newLogInfoMock = new NewLogInfoMock();
	private LogDataMock logDataMock = new LogDataMock();
	
	private String lastCategoryEnum = null;
	private String lastEventDate = null;
	private Long lastResponseTime = null;
	
	@BeforeTest
	public void before(){
	}
	
	@Test
	public void modifyRawDataTest(){
		SimpleLogLineParser underTest = new SimpleLogLineParser();
		underTest.modifyRawData(new ILogData[]{logDataMock}, "2011-10-04 00:02:02,754 [THREAD-3] myApp/servlet_1 was called successfully");
		underTest.modifyRawData(new ILogData[]{logDataMock}, "2011-10-04 00:02:03,628 [THREAD-3] RESPONSE_TIME:874");
		underTest.modifyRawData(new ILogData[]{logDataMock}, "2011-10-04 00:04:54,711 [THREAD-3] myApp/servlet_2 was called successfully");		
		underTest.modifyRawData(new ILogData[]{logDataMock}, "2011-10-04 00:04:55,885 [THREAD-3] RESPONSE_TIME:1174");
		Assert.assertEquals(lastEventDate, "2011-10-04 00:04:55,885");
		Assert.assertEquals(lastResponseTime, new Long(1174));
		Assert.assertEquals(lastCategoryEnum, "Feature_2");
	}
	
	class LogDataMock implements ILogData {

		public long getTo() {
			return dataCalculator.stringWithMilisToLong("2011-10-04 00:02:02,754");
		}

		public long getFrom() {
			return dataCalculator.stringWithMilisToLong("2011-10-04 00:04:55,886");
		}

		@Override
		public INewLogInfo getLogInfo(Long time) {
			return newLogInfoMock;
		}
		
	}

	class NewLogInfoMock implements INewLogInfo{

		@Override
		public void accept(String category, DateAndResponseTimeEntity dateAndResponseTime) {
			lastCategoryEnum = category;
			lastEventDate = dataCalculator.longToStringWithMilis(dateAndResponseTime.getDate());
			lastResponseTime = dateAndResponseTime.getResponseTime();
			
		}
		
	}
}
